package net.xo68.destiny.core.util;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * LocalDateTimeUtils
 * @author wuxie
 * @version 2020-10-19
 */
public final class LocalDateTimeUtils {

    /**
     * 默认时区
     */
    public static final ZoneOffset DEFAULT_ZONEOFFSET=ZoneOffset.of("+8");

    private LocalDateTimeUtils() {
        throw new IllegalStateException();
    }

    /**
     * 当前时间
     * @return
     */
    public static LocalDateTime now(){
        return LocalDateTime.now();
    }
    public static LocalDateTime now(ZoneOffset zoneOffset){
        return LocalDateTime.now(zoneOffset);
    }

    public static Date dateNow(){
        return dateNow(ZoneId.systemDefault());
    }
    public static Date dateNow(ZoneId zoneId){
        return toDate(LocalDateTime.now(), zoneId);
    }
    public static Date toDate(LocalDateTime localDateTime){
        return toDate(localDateTime, ZoneId.systemDefault());
    }
    public static Date toDate(LocalDateTime localDateTime, ZoneId zoneId){
        return Date.from(localDateTime.atZone(zoneId).toInstant());
    }

    /**
     * 转换为字符串
     * @param localDateTime
     * @param dateTimeFormatter
     * @return
     */
    public static String toString(LocalDateTime localDateTime, DateTimeFormatter dateTimeFormatter){

        return localDateTime.format(dateTimeFormatter);
    }
    public static String toString(LocalDateTime localDateTime, String dateTimeFormatter){

        return localDateTime.format(DateTimeFormatter.ofPattern(dateTimeFormatter));
    }
    public static String toString(LocalDate localDate, DateTimeFormatter dateTimeFormatter){

        return localDate.format(dateTimeFormatter);
    }
    public static String toString(LocalDate localDate, String dateTimeFormatter){

        return localDate.format(DateTimeFormatter.ofPattern(dateTimeFormatter));
    }

    /**
     * 字符串转换为 LocalDateTime
     * @param strDateTime
     * @param dateTimeFormatter
     * @return
     */
    public static LocalDateTime parseDateTime(String strDateTime, DateTimeFormatter dateTimeFormatter){
        return LocalDateTime.parse(strDateTime,dateTimeFormatter);
    }
    public static LocalDateTime parseDateTime(String strDateTime, String dateTimeFormatter){
        return LocalDateTime.parse(strDateTime,DateTimeFormatter.ofPattern(dateTimeFormatter));
    }

    public static LocalDate parseDate(String strDate, DateTimeFormatter dateTimeFormatter){
        return LocalDate.parse(strDate,dateTimeFormatter);
    }
    public static LocalDate parseDate(String strDate, String dateTimeFormatter){
        return LocalDate.parse(strDate,DateTimeFormatter.ofPattern(dateTimeFormatter));
    }

    public static LocalDateTime parseDateTime(Date date){
        Instant instant = date.toInstant();
        ZoneId zoneId  = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }
    public static LocalDate parseDate(Date date){
        Instant instant = date.toInstant();
        ZoneId zoneId  = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDate();
    }

    /**
     * 毫秒long 转换为 localdatetime
     * @param milliTime
     * @return
     */
    public static LocalDateTime parseDateTime(long milliTime){
        return parseDateTime(milliTime,ZoneId.systemDefault());
    }
    public static LocalDateTime parseDateTime(long milliTime, ZoneId zoneId){
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(milliTime),zoneId);
    }

    /**
     * 取得毫秒 long
     * @param localDateTime
     * @return
     */
    public static long toMilliTime(LocalDateTime localDateTime){
        return toMilliTime(localDateTime,ZoneId.systemDefault());
    }
    public static long toMilliTime(LocalDateTime localDateTime, ZoneId zoneId){
        return localDateTime.atZone(zoneId).toInstant().toEpochMilli();
    }

    /**
     * 长时间时间间隔计算
     * @param startTime
     * @param endTime
     * @return
     */
    public static Duration calculateTimespan(LocalDateTime startTime, LocalDateTime endTime){
        return Duration.between(startTime, endTime);
    }

    /**
     * 短时间时间间隔计算
     * @param startTime
     * @param endTime
     * @return
     */
    public static Period calculateTimespan(LocalDate startTime, LocalDate endTime){
        return Period.between(startTime, endTime);
    }
}
